<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>初探Cordova结合Vue | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Cordova,Vue,">
  

  <meta name="description" content="一、前言 我们可以使用cordova来打包现有的vue、react、angular应用为app，可以借助cordova来调用手机设备的原生能力，比如拍照、扫码、定位等  1.1 ionic3 ionic3=cordova+angular+ionicUI（Ionic UI组件+ Javascript API+Ionic Native）    优点：它提供了漂亮的UI组件库、强大的JS APi以及基于">
<meta name="keywords" content="Cordova,Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="初探Cordova结合Vue">
<meta property="og:url" content="http://blog.poetries.top/2019/10/07/cordova-for-vue/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、前言 我们可以使用cordova来打包现有的vue、react、angular应用为app，可以借助cordova来调用手机设备的原生能力，比如拍照、扫码、定位等  1.1 ionic3 ionic3=cordova+angular+ionicUI（Ionic UI组件+ Javascript API+Ionic Native）    优点：它提供了漂亮的UI组件库、强大的JS APi以及基于">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/2.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/3.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/4.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/5.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/6.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/7.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/8.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/9.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/10.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/11.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/12.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/14.png">
<meta property="og:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/16.png">
<meta property="og:updated_time" content="2020-08-15T04:25:31.902Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="初探Cordova结合Vue">
<meta name="twitter:description" content="一、前言 我们可以使用cordova来打包现有的vue、react、angular应用为app，可以借助cordova来调用手机设备的原生能力，比如拍照、扫码、定位等  1.1 ionic3 ionic3=cordova+angular+ionicUI（Ionic UI组件+ Javascript API+Ionic Native）    优点：它提供了漂亮的UI组件库、强大的JS APi以及基于">
<meta name="twitter:image" content="https://poetries1.gitee.io/img-repo/2019/10/cordova/2.png">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、前言"><span class="toc-text">一、前言</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-ionic3"><span class="toc-text">1.1 ionic3</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-cordova"><span class="toc-text">1.2 cordova</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、环境搭建"><span class="toc-text">二、环境搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-用cordova开发android-应用"><span class="toc-text">2.1 用cordova开发android 应用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-iOS平台搭建cordova环境"><span class="toc-text">2.2 iOS平台搭建cordova环境</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、cordova插件的使用"><span class="toc-text">三、cordova插件的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、cordova结合vue"><span class="toc-text">四、cordova结合vue</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-cordova结合vue开发跨平台app"><span class="toc-text">4.1 cordova结合vue开发跨平台app</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-vue调用cordova插件"><span class="toc-text">4.2 vue调用cordova插件</span></a></li></ol></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-cordova-for-vue" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">初探Cordova结合Vue</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2019.10.07</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 1.7k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 7分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、前言"><a href="#一、前言" class="headerlink" title="一、前言"></a>一、前言</h2><blockquote>
<p>我们可以使用<code>cordova</code>来打包现有的<code>vue</code>、<code>react</code>、<code>angular</code>应用为<code>app</code>，可以借助<code>cordova</code>来调用手机设备的原生能力，比如拍照、扫码、定位等</p>
</blockquote>
<h3 id="1-1-ionic3"><a href="#1-1-ionic3" class="headerlink" title="1.1 ionic3"></a>1.1 ionic3</h3><blockquote>
<p><code>ionic3=cordova+angular+ionicUI</code>（Ionic UI组件+ Javascript API+Ionic Native） </p>
</blockquote>
<ul>
<li><strong>优点</strong>：它提供了漂亮的<code>UI</code>组件库、强大的<code>JS APi</code>以及基于调用原生的的<code>Native APi</code>,可以让我们快速开发跨平台的混合<code>APP</code>以及移动<code>web</code>页面。（推荐*）</li>
<li><strong>缺点</strong>：<code>angular</code> <code>react</code> <code>vue</code>开发的移动端应用要打包成<code>app</code>的时候得重新再学习<code>ionic</code></li>
</ul>
<h3 id="1-2-cordova"><a href="#1-2-cordova" class="headerlink" title="1.2 cordova"></a>1.2 cordova</h3><blockquote>
<p><code>cordova</code>: 可以把<code>html css js</code>写的代码打包成<code>app</code>，还可以让<code>js</code>调用原生的<code>api</code>。<code>cordova</code>非常成熟、插件也非常多、扩展性也强，10年的历史</p>
</blockquote>
<p><strong>打包App有几个方案</strong></p>
<ul>
<li><code>ionic</code></li>
<li><code>reactNative</code></li>
<li><code>weex</code></li>
<li><code>flutter</code></li>
<li><code>cordova+vue</code></li>
<li><code>cordova+react</code></li>
<li><code>cordova+angular</code></li>
</ul>
<h2 id="二、环境搭建"><a href="#二、环境搭建" class="headerlink" title="二、环境搭建"></a>二、环境搭建</h2><h3 id="2-1-用cordova开发android-应用"><a href="#2-1-用cordova开发android-应用" class="headerlink" title="2.1 用cordova开发android 应用"></a>2.1 用cordova开发android 应用</h3><ul>
<li>安装jdk 、配置jdk</li>
<li>安装android studio</li>
<li>安装nodejs</li>
<li>安装cordova</li>
</ul>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment">## 淘宝源安装</span></span><br><span class="line">npm install -g cordova --registry=https://registry.npm.taobao.org</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cnpm install -g cordova</span><br></pre></td></tr></table></figure>
<p><strong>创建项目cordova create 项目名称</strong></p>
<blockquote>
<p><code>cordova create 项目名</code>  <code>com.公司名.项目名  类名</code> （建议）</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cordova create cordovademo02  com.baidu.cordova  Cordovademo</span><br></pre></td></tr></table></figure>
<blockquote>
<p>创建项目的时候注意包名称：发布上线打包的时候用到包名称，注意</p>
</blockquote>
<blockquote>
<p>修改应用包名名称参考：<a href="http://www.ionic.wang/article-index-id-91.html" target="_blank" rel="noopener">http://www.ionic.wang/article-index-id-91.html</a></p>
</blockquote>
<p><strong>修改应用包名名称：</strong></p>
<ul>
<li>修改<code>config.xml</code>里面的包名称</li>
<li>修改完成以后重新执行<code>cordova platform add android</code></li>
</ul>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/2.png" alt></p>
<blockquote>
<p>cd 到项目里面 <code>cd cordovademo02</code></p>
</blockquote>
<ul>
<li>把<code>android</code>的平台添加到项目里面 <code>cordova platform add android</code></li>
<li>把项目导入到 <code>android studio</code> 进行运行调试  （或者运行   <code>cordova  run  android</code>）   注意可能遇到的问题参考（安装遇到问题图文解决方案文件夹</li>
</ul>
<blockquote>
<p>导入<code>platform</code>下的<code>Android</code>文件</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/3.png" alt></p>
<p><strong>导入android studio可能遇到的错误解决方法</strong></p>
<ol>
<li>导入后提示：<code>Android Studio Error:Connection timed out: connect</code></li>
</ol>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/4.png" alt></p>
<blockquote>
<p>解决方案参考：<a href="https://blog.csdn.net/u013020000/article/details/73159754" target="_blank" rel="noopener">https://blog.csdn.net/u013020000/article/details/73159754</a></p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/5.png" alt><br><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/6.png" alt></p>
<ol start="2">
<li>遇到错误 <code>failed  to  find with hash string  &#39;android-26&#39;</code></li>
</ol>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/7.png" alt></p>
<blockquote>
<p>解决方案点击 图上蓝色链接进行安装</p>
</blockquote>
<ol start="3">
<li><code>Gradle build</code> 没有反应如</li>
</ol>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/8.png" alt><br><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/9.png" alt></p>
<blockquote>
<p>解决方案 ：点击<code>build</code>见图箭头。如果有下载内容 耐心等待 （30分钟-2小时）</p>
</blockquote>
<ol start="4">
<li>提示 <code>please configure Android SDK</code></li>
</ol>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/10.png" alt></p>
<blockquote>
<p>解决方案：点击蓝色 <code>configure</code>，然后选择对应的sdk   （前提是sdk已经安装）</p>
</blockquote>
<ol start="5">
<li>真机调试,手机连上没有反应</li>
</ol>
<ul>
<li>关闭或者卸载自己电脑上面的360手机助手或者其他连手机的软件</li>
<li>安装你手机对应的<code>sdk</code></li>
</ul>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/11.png" alt></p>
<blockquote>
<p>建议 <code>android 5-到android8</code> sdk都安装 （安装sdk  ： Tools-&gt;SDK Manager）</p>
</blockquote>
<ul>
<li>点击右上角对应箭头按钮配置</li>
</ul>
<blockquote>
<p>查看当前连接上的手机</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/12.png" alt></p>
<ul>
<li>手机必须开启调试模式（百度搜 xxx手机开启调试模式）</li>
<li>手机拔下来重启<code>android studio</code>，重新插入手机重试</li>
<li>百度搜（<code>android studio</code> 连不上手机…）</li>
</ul>
<p><strong>运行项目 ：注意</strong></p>
<ul>
<li><code>android</code>手机要连上电脑，并且 <code>android</code>手机必须开启调试模式</li>
<li><code>android studio</code> 必须得安装手机对应的<code>sdk</code></li>
<li>关闭360手机助手、xxx手机助手</li>
<li>修改项目:  运行<code>cordova prepare</code></li>
</ul>
<h3 id="2-2-iOS平台搭建cordova环境"><a href="#2-2-iOS平台搭建cordova环境" class="headerlink" title="2.2 iOS平台搭建cordova环境"></a>2.2 iOS平台搭建cordova环境</h3><ul>
<li>安装<code>nodejs</code> 安装<code>xcode</code></li>
<li>安装<code>cordova</code>    </li>
</ul>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cnpm install -g cordova</span><br></pre></td></tr></table></figure>
<ol>
<li>创建项目</li>
</ol>
<blockquote>
<p><code>cordova create 项目名  com.公司名.项目名  类名</code> （建议）</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cordova create cordovademo02  com.baidu.cordova  Cordovademo</span><br></pre></td></tr></table></figure>
<ul>
<li><ol start="2">
<li><code>cd  cordovademo02</code></li>
</ol>
</li>
<li>3.把ios的平台添加到项目里面  <code>cordova platform add ios</code></li>
<li><ol start="4">
<li>用xcode打开项目运行</li>
</ol>
</li>
</ul>
<h2 id="三、cordova插件的使用"><a href="#三、cordova插件的使用" class="headerlink" title="三、cordova插件的使用"></a>三、cordova插件的使用</h2><blockquote>
<p>cordova插件拍照插件 、定位插件、文件上传插件 以及vconsole开启真机（手机）调试模式</p>
</blockquote>
<p><strong>cordova安装插件</strong></p>
<blockquote>
<p>如果我们要在自己的<code>html</code>里面调用手机原生的功能（拍照、扫描二维码、获取地理位置…）,借助<code>cordova</code>的插件</p>
</blockquote>
<blockquote>
<p><code>cordova</code> 官网：<a href="https://cordova.apache.org/" target="_blank" rel="noopener">https://cordova.apache.org/</a></p>
</blockquote>
<p><strong>如何使用插件：</strong></p>
<ul>
<li>安装插件 <code>cordova plugin  add  插件名称</code></li>
<li>复制文档使用</li>
</ul>
<p><strong>查看已经安装的插件：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cordova plugin list</span><br></pre></td></tr></table></figure>
<p><strong>卸载插件：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cordova plugin rm  cordova-plugin-network-information</span><br></pre></td></tr></table></figure>
<blockquote>
<p>插件使用</p>
</blockquote>
<p><strong>1. 设备插件的使用</strong></p>
<blockquote>
<p><a href="https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/index.html" target="_blank" rel="noopener">https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/index.html</a></p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment">## 安装 看文档使用</span></span><br><span class="line">cordova plugin add cordova-plugin-device</span><br></pre></td></tr></table></figure>
<p><strong>2. 使用网络相关的插件</strong></p>
<blockquote>
<p><a href="https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-network-information/index.html" target="_blank" rel="noopener">https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-network-information/index.html</a></p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 安装 看文档使用</span></span><br><span class="line">cordova plugin add cordova-plugin-network-information</span><br></pre></td></tr></table></figure>
<p><strong>3. 定位插件</strong></p>
<blockquote>
<p><a href="https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/index.html" target="_blank" rel="noopener">https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/index.html</a></p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">cordova plugin add cordova-plugin-geolocation</span><br></pre></td></tr></table></figure>
<ul>
<li>注意改代码以后要运行：<code>cordova prepare</code></li>
<li>注意：要引入<code>cordova.js</code></li>
<li>注意：项目里面不要有中文文件夹、不要有<code>zip</code>包 、不要有中文文件</li>
</ul>
<p><strong>4. 拍照插件</strong></p>
<blockquote>
<p><a href="https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/index.html" target="_blank" rel="noopener">https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-camera/index.html</a></p>
</blockquote>
<blockquote>
<p>注意：<code>ios</code>拍照完成以后调用 <code>navigator.camera.cleanup(onSuccess, onFail)</code></p>
</blockquote>
<p><strong>5. 文件上传 或者下载</strong></p>
<ul>
<li>文件插件: <a href="https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html" target="_blank" rel="noopener">https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html</a></li>
<li>文件传输插件：<a href="https://www.npmjs.com/package/cordova-plugin-file-transfer" target="_blank" rel="noopener">https://www.npmjs.com/package/cordova-plugin-file-transfer</a></li>
</ul>
<h2 id="四、cordova结合vue"><a href="#四、cordova结合vue" class="headerlink" title="四、cordova结合vue"></a>四、cordova结合vue</h2><h3 id="4-1-cordova结合vue开发跨平台app"><a href="#4-1-cordova结合vue开发跨平台app" class="headerlink" title="4.1 cordova结合vue开发跨平台app"></a>4.1 cordova结合vue开发跨平台app</h3><blockquote>
<p><code>cordova</code>结合<code>vue</code>开发跨平台混合app之<code>Cordova</code>打包Vue项目</p>
</blockquote>
<p><strong>cordova打包vue项目：</strong></p>
<ul>
<li><code>cordova</code>: 可以把<code>html css js</code>写的代码打包成<code>app</code>，还可以让<code>js</code>调用原生的 <code>api</code></li>
</ul>
<blockquote>
<p><code>cordova+vue</code>、<code>cordova+react</code> 、<code>cordova+angular</code> 、 <code>cordova+jquery</code></p>
</blockquote>
<p><strong>创建vue项目的时候有两种方式：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">vue init webpack 项目名称</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">vue init webpack-simple  项目名称</span><br></pre></td></tr></table></figure>
<blockquote>
<p>正式发布<code>vue</code>的项目：（把<code>vue</code>项目打包成浏览器能解析的代码）</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm run build <span class="comment"># 把vue打包成浏览器能解析的代码</span></span><br></pre></td></tr></table></figure>
<p><strong>把vue项目用cordova打包成app：</strong></p>
<ul>
<li><code>npm run build</code> （注意：图片目录的路径）</li>
<li>把<code>vue</code>打包后的静态资源复制到<code>cordova</code>项目里面</li>
<li>运行 <code>cordova prepare</code></li>
</ul>
<p><strong>注意：</strong></p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">vue init webpack-simple  <span class="comment">#项目名称</span></span><br></pre></td></tr></table></figure>
<ul>
<li>修改：<code>webpack.config.js</code> 里面  <code>publicPath</code></li>
<li>把<code>publicPath: &#39;/dist/&#39;</code>    改为  <code>publicPath: &#39;dist/&#39;</code></li>
<li>修改<code>index</code>里面引入<code>dist</code>的路径,去掉前面的<code>/</code> (重要)</li>
</ul>
<p><strong>vue init webpack 项目名称</strong></p>
<ul>
<li>修改：<code>config/index.js</code> 把 <code>assetsPublicPath: &#39;/&#39;</code>,    </li>
<li>修改成 <code>assetsPublicPath: &#39;./&#39;</code></li>
</ul>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/14.png" alt></p>
<blockquote>
<p>ios下打开效果。这样很方便打包原有的项目为app</p>
</blockquote>
<p><img src="https://poetries1.gitee.io/img-repo/2019/10/cordova/16.png" alt></p>
<h3 id="4-2-vue调用cordova插件"><a href="#4-2-vue调用cordova插件" class="headerlink" title="4.2 vue调用cordova插件"></a>4.2 vue调用cordova插件</h3><blockquote>
<p><code>cordova</code>打包<code>vue</code>项目并在<code>Vue</code>中调用原生的<code>api</code>：</p>
</blockquote>
<p><strong>使用vue-cordova 插件（不推荐使用）</strong></p>
<blockquote>
<p><a href="https://github.com/kartsims/vue-cordova" target="_blank" rel="noopener">https://github.com/kartsims/vue-cordova</a>        </p>
</blockquote>
<ol>
<li><code>vue</code>项目安装 <code>npm install --save vue-cordova</code></li>
<li>在 <code>main.js</code> 引入插件并<code>use</code>插件</li>
</ol>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">import VueCordova from &apos;vue-cordova&apos;</span><br><span class="line">Vue.use(VueCordova)</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>调用插件注意在对应的组件需要引入 </li>
</ol>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> Vue = <span class="built_in">require</span>(<span class="string">'vue'</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">Vue.cordova.camera.getPicture(<span class="function">(<span class="params">imageURI</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">window</span>.alert(<span class="string">'Photo URI : '</span> + imageURI)</span><br><span class="line">&#125;, (message) =&gt; &#123;</span><br><span class="line">    <span class="built_in">window</span>.alert(<span class="string">'FAILED : '</span> + message)</span><br><span class="line">&#125;, &#123;</span><br><span class="line">    quality: <span class="number">50</span>,</span><br><span class="line">    destinationType: Vue.cordova.camera.DestinationType.FILE_URI</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>注意需要在<code>vue</code>项目 <code>index.html</code>引入 <code>cordova.js</code></li>
</ol>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"cordova.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>index.html</code> 引入<code>cordova.js</code>  并定义全局变量让<code>vue</code>组件里面直接使用<code>cordova</code>插件(推荐的使用方法）</p>
</blockquote>
<ul>
<li>在vue <code>index.html</code>引入<code>cordova.js</code> （注意顺序<code>cordova.js</code>放在<code>build.js</code>上面）</li>
<li>直接可以在组件里面使用<code>cordova</code>的<code>api</code>（注意<code>cordova</code>里面要安装<code>api</code>的插件）</li>
</ul>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2019/10/07/ionic-ios-camera/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2019/11/17/es6-review-summary/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
